package com.jackrain.nea.wms.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.wms.api.BaseWmsSaveSuplierIncrCmd;
import com.jackrain.nea.wms.mapper.WmsSqlMapper;
import com.jackrain.nea.wms.utils.WmsQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;


@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version="1.0", group = "wms")
public class BaseWmsSaveSuplierIncrCmdImpl extends CommandAdapter implements BaseWmsSaveSuplierIncrCmd {

    @Autowired
    private WmsQueryService wmsQueryService;

    @Autowired
    private WmsSqlMapper wmsSqlMapper;

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        if (null == param || param.size() == 0 || param.getJSONObject("data").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }
        log.info("BaseWmsSaveSuplierIncrCmdImpl--param-->{}",param);

        String mTable = param.getString("tablename");
        JSONObject mainObj = param.getJSONObject("data");

        String active = param.getString("active");
        String message = "该数据已存在中间表中!->";
        String messageTwo = "该数据已更新成功!->";
        String messageThere = "该数据插入成功!->";
        String messageDel = "该记录不存在!->";
        log.debug("BaseWmsSaveSuplierIncrCmdImpl--mainObj-->{}",mainObj);

        //获取参数
        String pro_code = mainObj.getString("PRO_CODE");
        String supplier_code = mainObj.getString("SUPPLIER_CODE");
        String isDefault = param.getString("IS_DEFAULT");

        //查询重复
        String[] fields = new String[]{"IS_ACTIVE"};
        String where = " pro_code = '"+pro_code+"' and supplier_code = '" + supplier_code +"'";
        List<HashMap> proCodeList = wmsSqlMapper.select8(mTable,fields,where);
        log.debug("BaseWmsSaveSuplierIncrCmdImpl--proCodeList-->{}",proCodeList);

        if (proCodeList.size() > 0) {
            String is_active = (String) proCodeList.get(0).get("IS_ACTIVE");
            //可用且保存 || 不可用且删除   不处理
            if(("Y".equals(is_active))&&("save".equals(active))||("N".equals(is_active))&&("del".equals(active))) {
                message +="[pro_code:" + pro_code + ",supplier_code:" + supplier_code+"]";
                vh.put("code", 0);
                vh.put("message", Resources.getMessage(message));
                return vh;
            }
            //可用且删除 || 不可用且保存   改状态
            if(("N".equals(is_active))&&("save".equals(active))||("Y".equals(is_active))&&("del".equals(active))) {
                JSONObject setKeys = new JSONObject();
                if("N".equals(is_active)){
                    setKeys.put("IS_ACTIVE","Y");
                }else {
                    setKeys.put("IS_ACTIVE","N");
                }
                wmsSqlMapper.update(mTable, setKeys, mainObj);
                messageTwo += "[pro_code:" + pro_code + ",supplier_code:" + supplier_code+"]";
                vh.put("code", 0);
                vh.put("message", Resources.getMessage(messageTwo));
                return vh;
            }
        }else{
            if("del".equals(active)){
                messageDel += "[pro_code:" + pro_code + ",supplier_code:" + supplier_code+"]";
                vh.put("code", 0);
                vh.put("message", Resources.getMessage(messageDel));
                return vh;
            }else {
                //保存插入
                mainObj.put("IS_DEFAULT",isDefault);
                HashMap cTableMap = new HashMap();
                cTableMap.put("tableName", mTable);
                cTableMap.put("insertKeys", mainObj);
                log.debug("BaseWmsSaveSuplierIncrCmdImpl--cTableMap-->{}",cTableMap);
                wmsQueryService.commandInsert(cTableMap);
                messageThere += "[pro_code:" + pro_code + ",supplier_code:" + supplier_code+"]";
                vh.put("code", 0);
                vh.put("message", Resources.getMessage(messageThere));
                return vh;
            }
        }
        vh.put("code", 0);
        vh.put("message", Resources.getMessage("更新失败"));
        return vh;
    }
}
